package com.kzd.gtgame.main.mine

import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ConvertUtils
import com.kzd.gtgame.R
import com.kzd.gtgame.app.URL_PAY_MEMBER
import com.kzd.gtgame.custom.setOnClickAntiShakeListener
import com.kzd.gtgame.custom.setTransitionStatus
import com.kzd.gtgame.jetpack.vm.MineViewModel
import com.kzd.gtgame.main.mine.adapter.MemberCenterCardAdapter
import com.kzd.gtgame.main.mine.adapter.MemberCenterContentAdapter
import com.kzd.gtgame.main.popup.PaymentBottomPopup
import com.kzd.gtgame.main.toMineRichTextActivityUrl
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewModeFragment
import kotlinx.android.synthetic.main.fragment_member_center.*
import java.lang.StringBuilder


class MemberCanterFragment : OwnerViewModeFragment<MineViewModel>() {

    companion object {
        fun newInstance(): MemberCanterFragment {
            return MemberCanterFragment()
        }
    }

    private val mMemberCenterCardAdapter = MemberCenterCardAdapter()

    private val mMemberCenterContentAdapter = MemberCenterContentAdapter()

    private val mPaymentBottomPopup: PaymentBottomPopup by lazy {
        PaymentBottomPopup(requireContext(), onCenterConfig = { _, type, centerConfig ->
            viewModel.mineMemberCenterOrder(
                centerConfig.id, centerConfig.money, type.toString()
            )
        })
    }

    override fun viewModel(): MineViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.fragment_member_center

    override fun initView() {
        rvMemberCenterCardBody.apply {
            layoutManager = LinearLayoutManager(context).apply {
                orientation = LinearLayoutManager.HORIZONTAL
            }
            if (itemDecorationCount > 0)
                return
            addItemDecoration(ItemDecoration())
        }.adapter = mMemberCenterCardAdapter.apply {
            setOnItemClickListener { _, _, position ->
                selectPosition = position
                val itemDate = getItem(position)
                mMemberCenterContentAdapter
                    .setNewInstance(itemDate.content)
                tvMemberCenterPrice.text = StringBuilder().append("¥\t")
                    .append(itemDate.money)
            }
        }

        rvMemberCenterContentBody.adapter = mMemberCenterContentAdapter

        tvMemberCenterPayment.setOnClickAntiShakeListener {
            val centerConfig = viewModel.memberCenterCardLiveData.value?.data?.data?.config
                ?: return@setOnClickAntiShakeListener
            if (centerConfig.isNullOrEmpty())
                return@setOnClickAntiShakeListener
            mPaymentBottomPopup.fillData(centerConfig[mMemberCenterCardAdapter.selectPosition])
                .showPopupWindow()
        }
    }

    override fun initData() {
        viewModel.mineMemberCenterCard()
    }

    override fun initViewMode() {
        viewModel.memberCenterCardLiveData.observe(viewLifecycleOwner, { response ->
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data?.config
                ?: return@observe
            mMemberCenterCardAdapter.setNewInstance(data)

            if (data.size <= 0) {
                return@observe
            }
            mMemberCenterContentAdapter
                .setNewInstance(data[0].content)
            tvMemberCenterPrice.text = StringBuilder().append("¥\t")
                .append(data[0].money)
        })
        viewModel.centerOrderLiveData.observe(viewLifecycleOwner, { response ->
            setTransitionStatus(response)
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            val url = StringBuilder(URL_PAY_MEMBER)
                .append("&id=").append(data).toString()
            toMineRichTextActivityUrl(
                title = "支付", url = url
            )
        })
    }


    inner class ItemDecoration : RecyclerView.ItemDecoration() {
        override fun getItemOffsets(
            outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State
        ) {
            val position: Int = parent.getChildAdapterPosition(view)
            if (position == 0) {
                outRect.left = ConvertUtils.dp2px(15f)
            }
            if (position == state.itemCount - 1) {
                outRect.right = ConvertUtils.dp2px(15f)
            } else {
                outRect.right = ConvertUtils.dp2px(13f)
            }
        }
    }

}